package com.woniuxy.util;

import lombok.SneakyThrows;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @program: bookstore
 * @description: 用户上下类，获取登录用户的信息（id）
 * @create: 2024-10-11 14:34
 **/
public class UserContext {
    /**
     * 获取jwt中用户id
     *
     * @return
     */
    @SneakyThrows
    public static Long getUserId() {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();
        String jwt = request.getHeader("authorization");


        Map userMap = JwtUtil.getUserMap(jwt);

        Long userId = (Long) userMap.get("id");
        return userId;
    }
}
